import time

from selenium.webdriver import Keys
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.support.wait import WebDriverWait


class BasePage:
    def __init__(self,driver):
        self.driver = driver

    def find(self, locator, timeout=10):
        """
        显示等待 + find_element
        它返回的就是 driver.find_element(...) 找到的 WebElement！
        """
        return WebDriverWait(self.driver, timeout).until(
            EC.presence_of_element_located(locator)
        )

    def click(self, locator):
        self.find(locator).click()

    def input(self, locator, text):
        el = self.find(locator)
        el.clear()
        el.send_keys(text)
        el.send_keys(Keys.TAB)

    def clear_input(self, locator):
        el = self.find(locator)
        el.clear()

    def get_text(self, locator):
        return self.find(locator).text

    def refresh_page(self):
        self.driver.refresh()
        time.sleep(1)

    def is_element_exist(self, locator, timeout=5):
        try:
            WebDriverWait(self.driver, timeout).until(
                EC.presence_of_element_located(locator)
            )
            return True
        except:
            return False